/******************************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.                       */
/*                                                                            */
/*  NAME      = db2_base_type.h                                               */
/*  NOTE      =                                                               */
/*  DATE      = 2014/09/28 by zhengxd                                         */
/******************************************************************************/
#ifndef _DB2_BASE_TYPE_H_
#define _DB2_BASE_TYPE_H_

#include "frmem.h"
#include "frstr.h"
#include "frcomm.h"
#include "frlist.h"
#include "frhash2.h"
#include "frlog.h"
#include "iconv2utf8.h"
#include "frsutl.h"
#include "frhex_dump.h"

#include "conntrack.h"
#include "module.h"
#include "frct_comm.h"
#include "frdb_cursor.h"
#include "frreport_comm.h"
#include "frbyte_buffer.h"

/* Not get code point(NULL) */
#define DRDA_CP_NULL          0x0000   //NULL

#define DRDA_CP_C004          0xC004   //协议未定义, 未知

/* DDM command start */
#define DRDA_CP_ACCRDB        0x2001   //AccessRDB
#define DRDA_CP_ACCSEC        0x106D   //Accesssecurity
#define DRDA_CP_BGNBND        0x2002   //Beginbinding of a package to an RDB
#define DRDA_CP_BNDCPY        0x2011   //Copy an Existing RDB Package
#define DRDA_CP_BNDDPLY       0x2016   //Deploy an Existing RDB Package
#define DRDA_CP_BNDSQLSTT     0x2004   //BindSQL Statement to an RDB package
#define DRDA_CP_CLSQRY        0x2005   //Close query
#define DRDA_CP_CNTQRY        0x2006   //Continue query
#define DRDA_CP_DRPPKG        0x2007   //DROPa package at an RDB
#define DRDA_CP_DSCRDBTBL     0x2012   //Describe RDB table
#define DRDA_CP_DSCSQLSTT     0x2008   //Describe SQL statement
#define DRDA_CP_ENDBND        0x2009   //Endbinding of a package to an RDB
#define DRDA_CP_EXCSAT        0x1041   //Exchange server attributes
//#define DRDA_CP_EXCSQLGET Undefined in DRDA todo?
#define DRDA_CP_EXCSQLIMM     0x200A   //Execute immediate SQL statement
#define DRDA_CP_EXCSQLSET     0x2014   //Set SQL Environment
#define DRDA_CP_EXCSQLSTT     0x200B   //Execute SQL statement
#define DRDA_CP_GETNXTCHK     0x2015   //Get Next Chunk
#define DRDA_CP_INTRDBRQS     0x2003   //Interrupt RDB Request
#define DRDA_CP_OPNQRY        0x200C   //Open query
#define DRDA_CP_PRPSQLSTT     0x200D   //Prepare SQL statement
#define DRDA_CP_RDBCMM        0x200E   //RDBcommit unit of work used by RUOW connections
#define DRDA_CP_RDBRLLBCK     0x200F   //RDBrollback unit of work used by RUOW connections
#define DRDA_CP_REBIND        0x2010   //Rebindan existing RDB package
#define DRDA_CP_SECCHK        0x106E   //Securitycheck
#define DRDA_CP_SNDPKT        0x1805   //Send Packet
#define DRDA_CP_SYNCCTL       0x1055   //Syncpoint control request used for DUOW and XA connections
#define DRDA_CP_SYNCRSY       0x1069   //Syncpoint resynchronization request used by DUOW connections
/* DDM command end */

/* Command data objects start */
#define DRDA_CP_BNDOPT        0x2405   //Bind Option
#define DRDA_CP_SQLSTTVRB     0x2419   //SQL Statement Variable Descriptions
#define DRDA_CP_OUTOVR        0x2415   //Output Descriptor
#define DRDA_CP_SQLTBLNAM     0x003E   //SQL Object Name
#define DRDA_CP_SQLDTA        0x2412   //SQL Program Variable Data
#define DRDA_CP_PRCNAM        0x2138   //Procedure Name
#define DRDA_CP_SQLATTR       0x2450   //SQL Statement Attributes
#define DRDA_CP_RDBNAM        0x2110   //RDB name
#define DRDA_CP_LOGNAM        0x1184   //Log Name
#define DRDA_CP_LOGTSTMP      0x1185   //Log Timestamp
#define DRDA_CP_SNAADDR       0x11E9   //SNA Address
#define DRDA_CP_IPADDR        0x11E8   //TCP/IP Address
#define DRDA_CP_TCPHOST       0x11DD   //TCP/IP Domain Qualified Host Name
#define DRDA_CP_CNNTKN        0x1070   //Connection Token
/* Command data objects end */

/* Reply data objects start */
#define DRDA_CP_SQLCARD       0x2408   //SQLCA reply data
#define DRDA_CP_ACCSECRD      0x14AC   //FOR ACCSEC
#define DRDA_CP_MGRLVLOVR     0x1C03   //Manager Level Overrides
#define DRDA_CP_MONITORRD     0x1C00   //Monitor Reply Data
#define DRDA_CP_QRYDTA        0x241B   //Query Answer Set Data
#define DRDA_CP_SQLDARD       0x2411   //SQLDA Reply Data
#define DRDA_CP_EXCSATRD      0x1443   //FOR EXCSAT
#define DRDA_CP_QRYDSC        0x241A   //Query Answer Set Description
#define DRDA_CP_SQLCINRD      0x240B   //SQL Result Set Column Information Reply Data
#define DRDA_CP_SQLDTARD      0x2413   //SQL Data Reply Data
#define DRDA_CP_SQLRSLRD      0x240E   //SQL Result Set Reply Data
#define DRDA_CP_SYNCCRD       0x1248   //Sync Point Control Reply
#define DRDA_CP_SYNCTYPE      0x1187   //Sync Point Operation Type
#define DRDA_CP_XARETVAL      0x1904   //XA Return Value
#define DRDA_CP_PRPHRCLST     0x1905   //List of Prepared and Heuristically Completed XIDs
#define DRDA_CP_RSYNCTYP      0x11EA   //Resync Type
#define DRDA_CP_UOWID         0x11AA   //Unit ofWork Identifier
#define DRDA_CP_UOWSTATE      0x11AC   //Unit of Work State
#define DRDA_CP_KERSECPPL     0x1C02   //Kerberos Security Principal
/* Reply data objects end */

/* Reply messages start */
#define DRDA_CP_ACCRDBRM      0x2201   //FOR ACCRDB
#define DRDA_CP_RDBUPDRM      0x2218   //RDB Update Reply Message
#define DRDA_CP_ENDQRYRM      0x220B   //End of Query
#define DRDA_CP_CMMRQSRM      0x2225   //Commitment Request
#define DRDA_CP_ENDUOWRM      0x220C   //End Unit of Work Condition
#define DRDA_CP_OPNQRYRM      0x2205   //Open Query Complete
#define DRDA_CP_RSLSETRM      0x2219   //RDB Result Set Reply Message
#define DRDA_CP_ENDDTARM      0x2214   //End of Data
#define DRDA_CP_CMDCMPRM      0x124B   //Command Processing Completed
#define DRDA_CP_CMDVLTRM      0x221D   //Command Violation
#define DRDA_CP_SECCHKRM      0x1219   //FOR ACCRDB(Security Check Reply Message)
/* Reply messages end */

/* RDBNFNRM is returned on:
*  1.EXCSAT in releases prior to Version 6
*  2:ACCSEC and SECCHK in Version 6
*/
/* Reply error messages start */
#define DRDA_CP_AGNPRMRM      0x1232   //Permanent Agent Error
#define DRDA_CP_CMDATHRM      0x121C   //Not Authorized to Command
#define DRDA_CP_CMDCHKRM      0x1254   //Command Check
#define DRDA_CP_CMDNSPRM      0x1250   //Command Not Supported
#define DRDA_CP_MGRDEPRM      0x1218   //Manager Dependency Error
#define DRDA_CP_PRCCNVRM      0x1245   //Conversational Protocol Error
#define DRDA_CP_PRMNSPRM      0x1251   //Parameter Not Supported
#define DRDA_CP_RDBACCRM      0x2207   //RDB Currently Accessed
#define DRDA_CP_RDBAFLRM      0x221A   //RDB Access Failed Reply Message
#define DRDA_CP_RDBATHRM      0x2203   //Not Authorized to RDB
#define DRDA_CP_RDBNFNRM      0x2211   //RDB Not Found
#define DRDA_CP_RSCLMTRM      0x1233   //Resource Limits Reached
#define DRDA_CP_SYNTAXRM      0x124C   //Data Stream Syntax Error
#define DRDA_CP_TRGNSPRM      0x125F   //Target Not Supported
#define DRDA_CP_VALNSPRM      0x1252   //Parameter Value Not Supported
#define DRDA_CP_INVRQSRM      0x123C //Invalid Request  todo 协议中未找到定义
#define DRDA_CP_ABNUOWRM      0x220D   //Abnormal End Unit of Work Condition
#define DRDA_CP_BGNBNDRM      0x2208   //Begin Bind Error
#define DRDA_CP_OBJNSPRM      0x1253   //Object Not Supported
#define DRDA_CP_PKGBPARM      0x2209   //RDB Package Binding Process Active
#define DRDA_CP_RDBNACRM      0x2204   //RDB Not Accessed
#define DRDA_CP_SQLERRRM      0x2213   //SQL Error Condition
#define DRDA_CP_DTAMCHRM      0x220E   //Data Descriptor Mismatch
#define DRDA_CP_PKGBNARM      0x2206   //RDB Package Binding Not Active
#define DRDA_CP_QRYNOPRM      0x2202   //Query Not Open
#define DRDA_CP_MGRLVLRM      0x1210   //Manager-level Conflict MINLVL 2
#define DRDA_CP_DSCINVRM      0x220A   //Invalid Descriptor
#define DRDA_CP_INTTKNRM      0x2210   //Interrupt Token Invalid
#define DRDA_CP_OPNQFLRM      0x2212   //Open Query Failure
#define DRDA_CP_QRYPOPRM      0x220F   //Query Previously Opened
/* Reply error messages end */

/* common object start */
#define DRDA_CP_SECTKNOVR     0x190B   //Security Token Override
#define DRDA_CP_SQLSTT        0x2414   //SQL Statement
#define DRDA_CP_TYPDEFNAM     0x002F   //Data Type Definition Name
#define DRDA_CP_TYPDEFOVR     0x0035   //TYPDEF Overrides
#define DRDA_CP_EXTDTA        0x146C   //Externalized FD:OCA Data
#define DRDA_CP_SECTKN        0x11DC   //Security token
#define DRDA_CP_PKTOBJ        0x1C04   //Packet Object
#define DRDA_CP_SYNCLOG       0x106F   //Sync Point Log
/* common object end */

/* 参数code point */
#define DRDA_CP_EXTNAM        0x115E   //External Name
#define DRDA_CP_MGRLVLLS      0x1404   //Manager-level List
#define DRDA_CP_SPVNAM        0x115D   //Supervisor Name
#define DRDA_CP_SRVCLSNM      0x1147   //Server Class Name
#define DRDA_CP_SRVNAM        0x116D   //Server Name
#define DRDA_CP_SRVRLSLV      0x115A   //Server Product Release Level
#define DRDA_CP_SECMGRNM      0x1196   //Security manager name
#define DRDA_CP_SECMEC        0x11A2   //Security mechanism
#define DRDA_CP_ENCALG        0x1909   //Encryption algorithm
#define DRDA_CP_ENCKEYLEN     0x190A   //Encryption key length
#define DRDA_CP_SVRCOD        0x1149   //Severity Code
#define DRDA_CP_SRVDGN        0x1153   //Server Diagnostic Information
//#define DRDA_CP_RECCNT     todo 协议中未找到定义
#define DRDA_CP_SRVLST        0x244E   //Server List
#define DRDA_CP_CODPNT        0x000C   //Codepoint
#define DRDA_CP_DEPERRCD      0x119B   //Manager Dependency Error Code
#define DRDA_CP_PRCCNVCD      0x113F   //Conversation Protocol Error Code
#define DRDA_CP_DCLNAM        0xD001   //Declared Name todo?
#define DRDA_CP_PRDID         0x112E   //Product-specific Identifier
#define DRDA_CP_RSCNAM        0x112D   //Resource Name Information
#define DRDA_CP_RSCTYP        0x111F   //Resource Type Information
#define DRDA_CP_RSNCOD        0x1127   //Reason Code Information
#define DRDA_CP_SYNERRCD      0x114A   //Syntax Error Code
#define DRDA_CP_PKGNAMCT      0x2112   //RDB Package Name and Consistency Token
#define DRDA_CP_VRSNAM        0x1144   //Version Name
#define DRDA_CP_RDBINTTKN     0x2103   //RDB Interrupt Token
#define DRDA_CP_PKGNAMCSN     0x2113   //RDB Package Name, Consistency Token, and Section Number
#define DRDA_CP_PLGINLST      0x190E   //Security Plug-in List
#define DRDA_CP_SECCHKCD      0x11A4   //Security Check Code
#define DRDA_CP_USRID         0x11A0   //User ID at the Target System
#define DRDA_CP_PASSWORD      0x11A1   //Password
#define DRDA_CP_USRORGID      0x1C06   //User Original User ID
#define DRDA_CP_USRREGNM      0x1C05   //User Registry Name
#define DRDA_CP_USRSECTOK     0x1C07   //User Security Tokens
#define DRDA_CP_ARMCORR       0x2161   //ARM Correlator
#define DRDA_CP_CRRTKN        0x2135   //Correlation Token
#define DRDA_CP_DIAGLVL       0x2160   //SQL Error Diagnostic Level
#define DRDA_CP_PRDDTA        0x2104   //Product-specific Data
#define DRDA_CP_RDBACCCL      0x210F   //RDB Access Manager Class
#define DRDA_CP_RDBALWUPD     0x211A   //RDB Allow Updates
#define DRDA_CP_STTDECDEL     0x2121   //Statement Decimal Delimiter
#define DRDA_CP_STTSTRDEL     0x2120   //Statement String Delimiter
#define DRDA_CP_TRGDFTRT      0x213B   //Target Default Value Return
#define DRDA_CP_TRUST         0x212C   //Access as a Trusted Entity
#define DRDA_CP_UNPUPDALW     0x2165   //Unprotected Update Allowed
#define DRDA_CP_PKGDFTCST     0x2125   //Package Default Character Subtype
#define DRDA_CP_CMDSRCID      0x2107   //Command Source Identifier
#define DRDA_CP_MONITOR       0x1900   //Monitor Events
#define DRDA_CP_PKGSN         0x210C   //RDB Package Section Number
#define DRDA_CP_RTNSQLDA      0x2116   //Return SQL Descriptor Area
#define DRDA_CP_TYPSQLDA      0x2146   //Type of SQL Descriptor Area
#define DRDA_CP_ENDREF        0x2163   // End of Progressive Reference
#define DRDA_CP_LSTCHK        0x2164   //Last Chunk of Data
#define DRDA_CP_UOWDSP        0x2115   //Unit of Work Disposition
#define DRDA_CP_RLSCONV       0x119F   //Release Conversation
#define DRDA_CP_QRYINSID      0x215B   //Query Instance Identifier
#define DRDA_CP_QRYCLSRLS     0x215E   //Query Close Lock Release
#define DRDA_CP_SVCERRNO      0x11B4   //Security Service Error Number
#define DRDA_CP_DUPQRYOK      0x210B   //Duplicate Query Allowed
#define DRDA_CP_DYNDTAFMT     0x214B   //Dynamic Data Format
#define DRDA_CP_MAXBLKEXT     0x2141   //Maximum Number of Extra Blocks
#define DRDA_CP_MEDDTASZ      0x2137   //Maximum Size of Medium Data
#define DRDA_CP_OUTOVROPT     0x2147   //Output Option for LOB Data
#define DRDA_CP_QRYBLKCTL     0x2132   //Query Block Protocol Control
#define DRDA_CP_QRYBLKSZ      0x2114   //Query Block Size
#define DRDA_CP_QRYCLSIMP     0x215D   //Query Close Implicit
#define DRDA_CP_QRYEXTDTASZ   0x2134   //Query Externalized Data Size
#define DRDA_CP_QRYROWSET     0x2156   //Query Rowset Size
#define DRDA_CP_SMLDTASZ      0x2136   //Maximum Size of Small Data
#define DRDA_CP_FREPRVREF     0x214C   //Free Previously Returned Progressive References
#define DRDA_CP_QRYBLKRST     0x2154   //Query Block Reset
#define DRDA_CP_QRYROWNBR     0x213D   //Query Row Number
#define DRDA_CP_QRYROWSNS     0x2153   //Query Row Sensitivity
#define DRDA_CP_QRYRTNDTA     0x2155   //Query Returns Data
#define DRDA_CP_QRYSCRORN     0x2152   //Query Scroll Orientation
#define DRDA_CP_RTNEXTDTA     0x2148   //Return of EXTDTAOption
#define DRDA_CP_MAXRSLCNT     0x2140   //maximum number of result sets
#define DRDA_CP_OUTEXP        0x2111   //output expected
#define DRDA_CP_RDBCMTOK      0x2105   //RDB commit by application server allowed
#define DRDA_CP_RSLSETFLG     0x2142   //result set flags
#define DRDA_CP_FDOEXT        0x147B   //FD:OCA Extent Data
#define DRDA_CP_FDOOFF        0x147D   //FD:OCA Offset Data
#define DRDA_CP_FDODSC        0x0010   //FD:OCA Data Descriptor
#define DRDA_CP_FDODTA        0x147A   //FD:OCA Data
#define DRDA_CP_CMMTYP        0x2143   //Commitment Request Type
#define DRDA_CP_RTNSETSTT     0x210E   //Return SET Statement
#define DRDA_CP_NBRROW        0x213A   //Number of Input Rows
#define DRDA_CP_PLGINID       0x190D   //Security Plug-in-specific Identifier
#define DRDA_CP_PLGINNM       0x190C   //Security Plug-in Name
#define DRDA_CP_NEWPASSWORD   0x11DE   //New Password
#define DRDA_CP_SLA           0x2166   //Service-Level Agreement
#define DRDA_CP_BUFINSIND     0x215C   //Buffered Insert Indicator
#define DRDA_CP_DFTFMTXML     0x245C   //Default Format for XML Data
#define DRDA_CP_OUTOVRXML     0x245A   //Output Option for XML Data
#define DRDA_CP_ATMIND        0x2159   //Atomicity Indicator
#define DRDA_CP_DSCERRCD      0x2101   //Description Error Code
#define DRDA_CP_FDODSCOFF     0x2118   //FD:OCA Descriptor Offset
#define DRDA_CP_FDOPRMOFF     0x212B   //FD:OCA Triplet Parameter Offset
#define DRDA_CP_FDOTRPOFF     0x212A   //FD:OCA Triplet Offset
#define DRDA_CP_FORGET        0x1186   //Forget Unit of Work
#define DRDA_CP_TIMEOUT       0x1907   //Time Out
#define DRDA_CP_XAFLAGS       0x1903   //XA Flags
#define DRDA_CP_XID           0x1801   //Global Transaction Identifier
#define DRDA_CP_XIDSHR        0x1802   //Share Recoverable Resources

/* 参数MGRLVLLS的参数code point */
#define DRDA_CP_AGENT         0x1403
#define DRDA_CP_CCSIDMGR      0x14CC
#define DRDA_CP_UNICODEMGR    0x1C08
#define DRDA_CP_CMNAPPC       0x1444
#define DRDA_CP_CMNSYNCPT     0x147C
#define DRDA_CP_CMNTCPIP      0x1474
#define DRDA_CP_DICTIONARY    0x1458
#define DRDA_CP_RDB           0x240F
#define DRDA_CP_RSYNCMGR      0x14C1
#define DRDA_CP_SECMGR        0x1440
#define DRDA_CP_SQLAM         0x2407
#define DRDA_CP_SUPERVISOR    0x143C
#define DRDA_CP_SYNCPTMGR     0x14C0
#define DRDA_CP_XAMGR         0x1C01

#define DRDA_DDM_HD_LEN        10     /* DDM header的长度 */

/* TYPEDEFN类型 */
#define DRDA_TYPDEF_UNKNOW     0
#define DRDA_TYPDEF_QTDSQL370  1
#define DRDA_TYPDEF_QTDSQLX86  2
#define DRDA_TYPDEF_QTDSQL400  3
#define DRDA_TYPDEF_QTDSQLASC  4
#define DRDA_TYPDEF_QTDSQLVAX  5

/* boolean值 */
#define DB2_FALSE              0
#define DB2_TRUE               1

/* packet类型 */
#define DB2_UNKNOW             0
#define DB2_REQUEST            1
#define DB2_RESPONSE           2

/* DDM类型 */
#define DB2_DDM                0
#define DB2_OBJ                1

#define DB2_CONTINUATION_BIT   0x8000


/* Command information */
typedef struct __st_db2_req_cmd {
    int i_new_flg;                        /* 新创建flag */
    int i_req_cmd_cp;                     /* Command code point */
    int i_close_flg;                      /* 关闭游标标识 */
    long l_cursor_id;                     /* session number */
    int i_error_code;                     /* 错误号 */
    char *p_error_msg;                    /* 错误信息 */
    int i_parsed_index;                   /* 解析到对应request Command的位置 */
    Sql_statement *p_st_statement;        /* prepare数据索引 */
    Sql_resultset *p_st_resultset;        /* cursor ID对应结果集索引 */
}Db2_req_cmd;

#endif
